var base64_decode = function(input) {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    if (input.length % 4 != 0) {
        return "";
    }
    var base64test = /[^A-Za-z0-9\+\/\=]/g;
    if (base64test.exec(input)) {
        return "";
    }
    do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output += String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output += String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);
    return output;
};

var base64_encode = function(str) {
    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while (i < len) {
        c1 = str.charCodeAt(i++) & 0xff;
        if (i == len) {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt((c1 & 0x3) << 4);
            out += "==";
            break;
        }
        c2 = str.charCodeAt(i++);
        if (i == len) {
            out += base64EncodeChars.charAt(c1 >> 2);
            out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            out += base64EncodeChars.charAt((c2 & 0xF) << 2);
            out += "=";
            break;
        }
        c3 = str.charCodeAt(i++);
        out += base64EncodeChars.charAt(c1 >> 2);
        out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
        out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

jQuery.extend({
    /**
     * 如果type=loading,则有返回值，
     * var loadingBox = $.tipsBoxy({type:"loading"});
     * 需调用loadingBox.cancel();关闭
     */
    tipsBoxy: function(options) {
        options = jQuery.extend({
            message: "",
            type: "success",
            time: 1500,
            width: 164,
            height: 68,
            callback: false,
            mask: false
        }, options);
        var IE6_scollheight = 0;
        if ($.browser.msie && $.browser.version == '6.0') {
            IE6_scollheight = document.documentElement.scrollTop;
        }
        boxy = {

            init: function() {
                var tipsImg = '';

                switch (options.type) {
                    case 'success':
                        this.tipsImg = '<img src="/images/ico_ok_25x25.png" class="vm mr10">';
                        break;
                    case 'error':
                        this.tipsImg = '<img src="/images/ico_error_25x25.png" class="vm mr10">';
                        break;
                    case 'notice':
                        this.tipsImg = '<img src="/images/ico_warning_24x25.png" class="vm mr10">';
                        break;
                    case 'mailtip':
                        this.tipsImg = '<img src="/images/ico_ok_16x16.png"  class="pr mr10" style="top:-2px;*top:0px;">';
                        break;
                    case 'loading':
                        this.tipsImg = '<img src="/images/loading.gif" class="vm mr10" />';
                }

                var html = [
                    options.mask ? '<div class="fe_window_mask" id="fe_window_mask" style="display:block;"><iframe class="fe_window_iframe" frameborder="0"></iframe></div>' : '',
                    '<div class="fe_dialogBox" id="fe_dialogBox" style="width:' + options.width + 'px;height:' + options.height + 'px;margin:-' + (options.height / 2) + 'px 0 0 -' + (options.width / 2) + 'px;_margin-top:' + parseInt(IE6_scollheight - options.height / 2) + 'px">',
                    '<div class="fe_dialog" style="width:' + (options.width - 14) + 'px;height:' + (options.height - 14) + 'px;">',
                    '<div class="contains">',
                    '<div class="bd">',
                    '<p class="lh25 mt15 tc vm f14 c_777">',
                    this.tipsImg,
                    '<span>' + options.type == 'loading' ? '' : options.message + '</span>',
                    '</p></div>',
                    '</div></div></div>'
                ].join('');
                if ($("#fe_dialogBox").length > 0) {
                    $("#fe_dialogBox div.bd").html('<p class="lh25 mt15 tc vm f14 c_777">' + this.tipsImg + '<span>' + options.message + '</span></p>');
                } else {
                    $("body").append(html);
                }
            },

            cancel: function() {
                $("#fe_dialogBox").hide().remove();
                $("#fe_window_mask").remove();
                if (options.callback != false) {
                    options.callback();
                }
            }
        }

        boxy.init();
        if (options.type != 'loading') {
            setTimeout("boxy.cancel()", options.time);
        } else {
            return boxy;
        }
    },
    alertBoxy: function(options) {
        options = jQuery.extend({
            message: "注意",
            title: "提示",
            width: 352,
            height: 192,
            callback: false
        }, options);
        var IE6_scollheight = 0;
        if ($.browser.msie && $.browser.version == '6.0') {
            IE6_scollheight = document.documentElement.scrollTop;
        }
        var html = [
            '<div class="fe_window_mask" id="fe_window_mask" style="display:block;"><iframe class="fe_window_iframe" frameborder="0"></iframe></div>',
            '<div class="fe_dialogBox" id="fe_dialogBox" style="width:' + options.width + 'px;height:' + options.height + 'px;margin:-' + (options.height / 2) + 'px 0 0 -' + (options.width / 2) + 'px;_margin-top:' + parseInt(IE6_scollheight - options.height / 2) + 'px">',
            '<div class="fe_dialog" style="width:' + (options.width - 14) + 'px;height:' + (options.height - 14) + 'px;">',
            '<div class="contains">',
            '<div class="hd">' + options.title + '</div>',
            '<div class="bd">',
            '<p class="lh25 mt50 tc vm f14 c_777">',
            '<img alt="" src="/images/ico_warning_24x25.png" class="vm mr10">',
            '<span>' + options.message + '</span>',
            '</p></div>',
            '</div><a href="javascript:;" class="close">关闭</a></div></div>'
        ].join('');
        $("body").append(html);
        $("#fe_dialogBox a.close").unbind().bind('click', function() {
            $("#fe_window_mask").remove();
            $("#fe_dialogBox").hide().remove();
            if (options.callback != false) {
                options.callback();
            }
        });
    },
    ckBoxy: function(options) {
        options = jQuery.extend({
            content: '',
            title: false,
            width: false,
            height: false,
            callback: false
        }, options);
        var IE6_scollheight = 0;
        if ($.browser.msie && $.browser.version == '6.0') {
            IE6_scollheight = document.documentElement.scrollTop;
        }
        var boxy = {
            html: [
                '<div class="fe_window_mask" id="fe_window_mask" style="display:block;"><iframe class="fe_window_iframe" frameborder="0"></iframe></div>',
                '<div class="fe_dialogBox" id="fe_dialogBox" style="width:' + options.width + 'px;height:' + options.height + 'px;margin:-' + (options.height / 2) + 'px 0 0 -' + (options.width / 2) + 'px;_margin-top:' + parseInt(IE6_scollheight - options.height / 2) + 'px">',
                '<div class="fe_dialog" style="width:' + (options.width - 14) + 'px;height:' + (options.height - 14) + 'px;">',
                '<div class="contains">',
                '<div class="hd">' + options.title + '</div>',
                '<div class="bd">' + options.content + '</div>',
                '</div><a href="javascript:;" class="close">关闭</a></div></div>'
            ].join(''),

            init: function() {
                $("body").append(boxy.html);
                $("#fe_dialogBox a.close").unbind().bind('click', function() {
                    boxy.cancel();
                    return false;
                });
                if (options.callback != false) {
                    options.callback();
                }
            },

            cancel: function() {
                $("#fe_window_mask").remove();
                $("#fe_dialogBox").hide().remove();
            }
        };
        boxy.init();
        return boxy;
    },
    confirmBoxy: function(options) {
        options = jQuery.extend({
            message: "您确定要删除此条记录吗？",
            title: "提示",
            width: 352,
            height: 192,
            ok: function() {},
            cancel: function() {},
            src: "/images/ico_warning_24x25.png"
        }, options);
        var IE6_scollheight = 0;
        if ($.browser.msie && $.browser.version == '6.0') {
            IE6_scollheight = document.documentElement.scrollTop;
        }
        var html = [
            '<div class="fe_window_mask" id="fe_window_mask" style="display:block;"><iframe class="fe_window_iframe" frameborder="0"></iframe></div>',
            '<div class="fe_dialogBox" id="fe_dialogBox" style="width:' + options.width + 'px;height:' + options.height + 'px;margin:-' + (options.height / 2) + 'px 0 0 -' + (options.width / 2) + 'px;_margin-top:' + parseInt(IE6_scollheight - options.height / 2) + 'px">',
            '<div class="fe_dialog" style="width:' + (options.width - 14) + 'px;height:' + (options.height - 14) + 'px;">',
            '<div class="contains">',
            '<div class="hd">' + options.title + '</div>',
            '<div class="bd">',
            '<p class="lh25 p20 tl vm f14 c_777">',
            '<img alt="" src="/images/ico_warning_24x24.png" class="vm mr10">',
            '<span>' + options.message + '</span>',
            '</p><div class="tc">',
            '<a class="c_btn30 mr20 submit" href="javascript:;"><span>确定</span></a>',
            '<a class="c_btn30 cancel" href="javascript:;"><span>取消</span></a>',
            '</div></div>',
            '</div><a href="javascript:;" class="close">关闭</a></div></div>'
        ].join('');

        $("body").append(html);
        $("#fe_dialogBox a.close,a.cancel").unbind().bind('click', function() {
            $("#fe_window_mask").remove();
            $("#fe_dialogBox").hide().remove();
            options.cancel();
        });
        $("#fe_dialogBox a.submit").unbind().bind('click', function() {
            $("#fe_window_mask").remove();
            $("#fe_dialogBox").hide().remove();
            options.ok();
        });
    },
});